package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ClientInfoStatus;
import java.util.List;

/**
 * @Name DishController
 * @Author stella
 * @Date 2023-03-08 3:00 PM
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取菜品列表
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public ResultInfo findOne( Long categoryId, Integer status){
        List<Dish> list =null;
        //先查询redis缓存
        //设置redis key
        String dishkey = "dish_" + categoryId;
       //判断key是否存在
        if (redisTemplate.hasKey(dishkey)){
            //存在，查询redis
            log.info("查询redis");
            list = ( List<Dish> ) redisTemplate.opsForValue().get(dishkey);
        }else{
            //不存在，查询mysql
            log.info("查询mysql");

            list = dishService.findByCategoryId(categoryId,status);
            //同步到redis
            redisTemplate.opsForValue().set(dishkey,list);
        }
        return ResultInfo.success(list);
    }
}
